package cn.new18.testactivitytask.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.new18.testactivitytask.R;
import cn.new18.testactivitytask.bFragment.fragb.TabFragA;
import cn.new18.testactivitytask.bFragment.fragb.TabFragB;
import cn.new18.testactivitytask.bFragment.fragb.TabFragC;
import cn.new18.testactivitytask.bFragment.fragb.TabFragD;

/**
 * Created by RaoWei on 2016/11/11 18:41.
 */

public class D extends FragmentActivity {
    private static final String TAG = "D";
    @BindView(R.id.b_fragb_container)
    FrameLayout mBFragbContainer;
    @BindView(R.id.b_fragb_radiogourp_1)
    RadioButton mBFragbRadiogourp1;
    @BindView(R.id.b_fragb_radiogourp_2)
    RadioButton mBFragbRadiogourp2;
    @BindView(R.id.b_fragb_radiogourp_3)
    RadioButton mBFragbRadiogourp3;
    @BindView(R.id.b_fragb_radiogourp_4)
    RadioButton mBFragbRadiogourp4;
    @BindView(R.id.b_fragb_radiogourp)
    RadioGroup mBFragbRadiogourp;
    private Fragment[] mFragList;
    private String[] mTag;
    private int mCurrentTab = 0;
    private int mLastTab = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.d);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        Log.e(TAG, this.getClass().toString() + ", pagePreparedToUser");
        initView();
        initEvent();
    }

    private void initEvent() {
        mBFragbRadiogourp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                mCurrentTab = turnIdToTabPosition(i);
                checkCurrentTab(mCurrentTab);
            }
        });
    }

    private boolean checkCurrentTab(int currentTab) {
        if (mLastTab == currentTab) {
            return false;
        }
        mLastTab = currentTab;
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        for (int i = 0; i < mFragList.length; i++) {
            if (mFragList[i].isAdded()) {
                fragmentTransaction.hide(mFragList[i]);
            }
        }
        if (!mFragList[currentTab].isAdded()) {
            fragmentTransaction.add(R.id.b_fragb_container, mFragList[currentTab], mTag[currentTab]);
        }
        fragmentTransaction.show(mFragList[currentTab]);
        fragmentTransaction.commit();
        return true;
    }

    private void initView() {
        mFragList = new Fragment[]{
                TabFragA.getInstance(),
                TabFragB.getInstance(),
                TabFragC.getInstance(),
                TabFragD.getInstance()};
        mTag = new String[]{"home", "live", "find", "me"};
        mBFragbRadiogourp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                Log.e(TAG, "radioGroup.i = " + i);
            }
        });
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (!mFragList[mLastTab].isAdded()) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.b_fragb_container, mFragList[mLastTab], mTag[mLastTab]);
            fragmentTransaction.commit();
        }
            mBFragbRadiogourp.check(turnTabPositionToId(mLastTab));
    }

    private int turnIdToTabPosition(int id) {
        int tab = 0;
        switch (id) {
            case R.id.b_fragb_radiogourp_1:
                tab = 0;
                break;
            case R.id.b_fragb_radiogourp_2:
                tab = 1;
                break;
            case R.id.b_fragb_radiogourp_3:
                tab = 2;
                break;
            case R.id.b_fragb_radiogourp_4:
                tab = 3;
                break;
        }
        return tab;
    }

    private int turnTabPositionToId(int tab) {
        int id = 0;
        switch (tab) {
            case R.id.b_fragb_radiogourp_1:
                id = 0;
                break;
            case R.id.b_fragb_radiogourp_2:
                id = 1;
                break;
            case R.id.b_fragb_radiogourp_3:
                id = 2;
                break;
            case R.id.b_fragb_radiogourp_4:
                id = 3;
                break;
        }
        return id;
    }
}
